<template>
	<view>
		<view class="header">
			<view class="integral_info">
				<view class="navbar">
					<view class="navbar-fixed">
						<!-- 状态栏小程序撑起高度 -->
						<view :style="{ height: statusBarHeight + 'px' }"></view>
						<view class="navbar-content" :style="{ height: navBarHeight + 'px', width: windowWidth + 'px' }">
							<image src="../../static/jt6.png" @click="back()"></image>
							<view class="navbar-search">我的积分</view>
						</view>
					</view>
					<!-- 需要添加占位符高度  状态栏高度+导航栏高度（否则下面tab会塌陷）indexOf-->
					<view :style="{ height: statusBarHeight + navBarHeight + 'px' }"></view>
				</view>
				<view class="scoreinfo">
					<view class="num">{{ parseFloat(this.myinfo.integral) }}</view>
					<view class="but" @click="tomoney()">充值</view>
				</view>
			</view>
			<view class="navlist">
				<view class="swiperbox">
					<scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll">
						<view v-for="(item, index) in catelist" :key="index" class="scroll-view-item_H" @click="handleone(index, item)">
							<view :style="{ color: index == currone ? '#222' : '#555', 'font-size': index == currone ? '36rpx' : '30rpx' }">
								{{ item.cate_name }}
							</view>
						</view>
					</scroll-view>
				</view>
				<view class="allcate" @click="open()">
					<view>全部</view>
					<image src="../../static/all.png"></image>
				</view>
			</view>
		</view>
		<view style="height: 472rpx"></view>
		<uni-popup ref="popup" type="top" background-color="#fff">
			<view class="tiptype">
				<view class="boxitem">
					<view class="itembox" v-for="(item, index) in catelist" :key="index" @click="changebx(item, index)" :style="{ color: index == currone ? '#FFF' : '', border: index == currone ? '2rpx solid #FF8A9B' : '', background: index == currone ? ' #FF8A9B' : '' }">
						{{ item.cate_name }}
					</view>
				</view>
			</view>
		</uni-popup>
		<view class="scorebox">
			<view class="scoreitem" v-for="(item, index) in list" :key="index" @click="todetail(item)">
				<image :src="item.url"></image>
				<view click="namebox">
					{{ item.goods_name }}
				</view>
				<view class="price">{{ item.goods_price_min }}<text style="font-size: 24rpx">积分</text></view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			statusBarHeight: 20,
			/* 状态栏高度 */
			navBarHeight: 45,
			/* 导航栏高度 */
			windowWidth: 375,
			/* 窗口宽度 */
			/* 设定状态栏默认高度 */
			scrollTop: 0,
			old: {
				scrollTop: 0,
			},
			catelist: [],
			currone: "",
			list: [],
			id_data: true,
			cate_id: "",
			myinfo: {},
		}
	},
	onShow() {
		this.getnav()
	},
	onLoad() {
		this.getcate()
		this.getmyinfo()
	},
	onReachBottom() {
		this.id_data && this.getgoods(this.cate_id)
	},
	methods: {
		getnav() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
			this.statusBarHeight = info.statusBarHeight
			this.windowWidth = info.windowWidth
			// 除了h5 app mp-alipay的情况下执行
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo)
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			this.navBarHeight = menuButtonInfo.bottom - info.statusBarHeight + (menuButtonInfo.top - info.statusBarHeight)
			this.windowWidth = menuButtonInfo.left
			// #endif
		},
		back() {
			uni.navigateBack({
				delta: 1,
			})
		},
		scroll: function (e) {
			// console.log(e)
			this.old.scrollTop = e.detail.scrollTop
		},
		handleone(index, item) {
			this.page = 1
			this.list = []
			this.currone = index
			this.cate_id = item.id
			this.getgoods(item.id)
		},
		getmyinfo() {
			this.$http({
				url: "/api/Course/user_info",
			}).then((res) => {
				this.myinfo = res.data
				//identity 1普通用户2志愿者3老师
			})
		},
		changebx(item, index) {
			this.currone = index
			this.cate_id = item.id
			this.getgoods(item.id)
			this.$refs.popup.close()
		},
		getcate() {
			this.$http({
				url: "/api/Wechat/cateList",
			}).then((res) => {
				this.catelist = res.data
				this.currone = this.catelist.findIndex((item) => item == this.cate_id)
				this.getgoods(this.cate_id)
			})
		},
		getgoods(id) {
			this.$http({
				url: "/api/Wechat/getGoodsByCateId",
				data: {
					cate_id: id || "",
					page: this.page,
					limit: 20,
					is_points_discount: 1, //0获取普通商品 1获取积分商品
				},
			}).then((res) => {
				if (res.data.length) {
					this.list.push(...res.data)
					this.list.forEach((item) => {
						item.num = 0
					})
					console.log(this.list)
					this.page++
					return
				}
				this.is_data = false
			})
		},
		open() {
			this.$refs.popup.open("top")
		},
		todetail(item) {
			uni.navigateTo({
				url: "/pageshop/goodsinfo/goodsinfo?id=" + item.id,
			})
		},
		tomoney() {
			uni.navigateTo({
				url: "/pageshop/recharge/recharge",
			})
		},
	},
}
</script>

<style lang="scss" scoped>
.header {
	position: fixed;
	top: 0;
	width: 750rpx;
	z-index: 999;
	.integral_info {
		width: 750rpx;
		height: 372rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		color: #ffffff;
		background-image: url($uni-url + "/bg55.png");
		background-size: 100% 100%;
		margin: 0 auto;

		.navbar {
			.navbar-fixed {
				position: fixed;
				top: 0;
				left: 0;
				z-index: 399;
				width: 100%;
				// height: 102rpx;
				// padding-bottom: 20rpx;
				background: transparent;

				.navbar-content {
					display: flex;
					// justify-content: center;
					align-items: center;
					padding: 0rpx 30rpx;
					// height: 142rpx;
					box-sizing: border-box;

					image {
						width: 44rpx;
						height: 44rpx;
						margin-right: 20rpx;
					}

					.navbar-search {
						display: flex;
						// align-items: center;
						// padding: 0 10px;
						font-size: 36rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #222;
						// position: absolute;
						// left: 50%;
						// transform: translate(-50%,0);
					}
				}
			}
		}
		.scoreinfo {
			display: flex;
			align-items: center;
			height: 350rpx;
			width: 650rpx;
			justify-content: space-between;
			margin: 0 auto;
			.num {
				font-size: 52rpx;
				font-family: Source Han Sans CN-Bold, Source Han Sans CN;
				font-weight: bold;
				color: #222222;
			}
			.but {
				width: 178rpx;
				height: 70rpx;
				background: linear-gradient(180deg, #ff6178 0%, #ff8a9b 100%);
				box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(255, 138, 155, 0.25);
				border-radius: 36rpx;
				color: #fff;
				text-align: center;
				line-height: 70rpx;
			}
		}
	}
	.navlist {
		display: flex;
		align-items: center;
		background-color: #fff;
		height: 122rpx;
		border-radius: 22rpx 22rpx 0 0;
		margin-top: -22rpx;
		width: 750rpx;
		.swiperbox {
			width: 628rpx;
			display: flex;
			align-items: center;
			.scroll-view_H {
				white-space: nowrap;
				width: 100%;
				.scroll-view-item_H {
					display: inline-block;
					width: fit-content;
					padding: 0 20rpx;
					height: 122rpx;
					// line-height: 122rpx;

					> view {
						line-height: 122rpx;
						text-align: center;
						font-size: 30rpx;
						font-family: SourceHanSansCN-Medium-, SourceHanSansCN-Medium;
						font-weight: normal;
						color: #222;
					}
				}
			}
		}
		.allcate {
			width: 128rpx;
			height: 122rpx;
			display: flex;
			align-items: center;
			font-size: 30rpx;
			font-family: Source Han Sans CN-Medium, Source Han Sans CN;
			font-weight: 500;
			color: #555555;
			image {
				width: 26rpx;
				height: 27rpx;
				margin-left: 6rpx;
			}
		}
	}
}
.tiptype {
	width: 100%;
	padding-top: 472rpx;
	background-color: #fff;

	.boxitem {
		display: flex;
		flex-wrap: wrap;
		width: 690rpx;
		padding: 40rpx 30rpx;

		.itembox {
			height: 72rpx;
			border-radius: 40rpx;
			border: 2rpx solid #999999;
			line-height: 72rpx;
			width: fit-content;
			padding: 0 50rpx;
			margin-right: 24rpx;
			margin-bottom: 20rpx;
		}
	}
}
.scorebox {
	display: flex;
	width: 690rpx;
	padding: 0 30rpx;
	flex-wrap: wrap;
	justify-content: space-between;
	.scoreitem {
		width: 336rpx;
		height: 520rpx;
		background: #ffffff;
		border-radius: 20rpx;
		font-size: 32rpx;
		font-family: Source Han Sans CN-Medium, Source Han Sans CN;
		font-weight: 500;
		color: #222222;
		margin-bottom: 20rpx;
		image {
			width: 336rpx;
			height: 336rpx;
			border-radius: 20rpx 20rpx 0rpx 0rpx;
		}
		.namebox {
			width: 300rpx;
			margin: 10rpx auto;
			-webkit-line-clamp: 2;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			overflow: hidden;
			text-overflow: ellipsis;
		}
		.price {
			width: 300rpx;
			margin: 10rpx auto;
			color: #ff6178;
			font-size: 36rpx;
			font-family: Source Han Sans CN-Medium, Source Han Sans CN;
			font-weight: 500;
		}
	}
}
</style>
